package com.ruoyi.boat.aop;

import com.ruoyi.boat.ws.WebsocketService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class WebsocketNotificationAspect {

    @Autowired
    WebsocketService websocketService;

    @AfterReturning(pointcut = "@annotation(websocketNotification)", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, WebsocketNotification websocketNotification, Object jsonResult) {
        handleLog(joinPoint, websocketNotification, jsonResult);
    }

    protected void handleLog(final JoinPoint joinPoint, WebsocketNotification websocketNotification, Object jsonResult) {
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        log.debug(className + "." + methodName + "()" + " websocketNotification: " + websocketNotification.value());
        websocketService.sendNotification(websocketNotification.value());
    }
}
